/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.server.server.serverpackets;

import java.util.ArrayList;
import java.util.List;

import l1j.opqlo.Server.L1World.L1World;
import l1j.server.Config;
import l1j.server.server.datatables.RaceTicketTable;
import l1j.server.server.datatables.ShopTable;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.game.L1BugBearRace;
import l1j.server.server.model.shop.L1AssessedItem;
import l1j.server.server.model.shop.L1Shop;
import l1j.server.server.templates.L1RaceTicket;

public class S_ShopBuyList extends ServerBasePacket {

	private static final String S_SHOP_BUY_LIST = "[S] S_ShopBuyList";

	public S_ShopBuyList(final int objid, final L1PcInstance pc) {
		final L1Object object = L1World.getInstance().findObject(objid);
		if (!(object instanceof L1NpcInstance)) {
			return;
		}
		final L1NpcInstance npc = (L1NpcInstance) object;
		final int npcId = npc.getNpcTemplate().get_npcId();
		final L1Shop shop = ShopTable.getInstance().get(npcId);

		// TODO 全道具販賣by eric1300460
		final List<L1AssessedItem> assessedItems;
		if (shop == null) {
			pc.sendPackets(new S_NoSell(npc));
			return;
		}
		if (Config.AllSell) {
			assessedItems = shop.allAssessItems(pc.getInventory());
			if (assessedItems.isEmpty()) {
				pc.sendPackets(new S_NoSell(npc));
				return;
			}
		} else {
			assessedItems = shop.assessItems(pc.getInventory());
			if (assessedItems.isEmpty()) {
				pc.sendPackets(new S_NoSell(npc));
				return;
			}
		}
		// TODO 全道具販賣by eric1300460

		this.writeC(S_OPCODE_SHOWSHOPSELLLIST);
		this.writeD(objid);
		L1RaceTicket ticket;// opqlo 賭肥支票
		boolean msg = false;
		final List<L1AssessedItem>itemList = new ArrayList<L1AssessedItem>();
		for (final L1AssessedItem item : assessedItems) {
			ticket = RaceTicketTable.getInstance().getTemplate(item.getTargetId());// opqlo 賭肥支票
			if (ticket != null) {// 賭票TABLE中有紀錄L1BugBearRace
				if (item.getItemId() == 40309) {
					if (ticket.get_round() != L1BugBearRace.getInstance().getRound() //票的場次與現在正進行中場次不符
							&&	ticket.get_allotment_percentage()* ticket.get_victory() == 0) {//沒有中獎的票
						pc.getInventory().removeItem(item.getItemInstance());
						msg = true;
						continue;// opqlo 不列入販售清單中
					}
					// 道具是賭票但是NPC編號不對
					if (npcId != 70035 && npcId != 70041 && npcId != 70042) {
						continue;// 不列入販售清單中
					}
				}else{ // 有紀錄但是不是賭票
					RaceTicketTable.getInstance().deleteTicket(item.getTargetId());
				}
			} else {
				if (item.getItemId() == 40309) {
					pc.getInventory().removeItem(item.getItemInstance());
					msg = true;
					continue; // 不列入販售清單中
				// 道具是賭票但是NPC編號不對
				}else if (npcId == 70035 || npcId == 70041 || npcId == 70042) {
					continue;// 不列入販售清單中
				}
			}
			itemList.add(item);
		}

		this.writeH(itemList.size());
		for(final L1AssessedItem item : itemList){
			this.writeD(item.getTargetId());
			this.writeD(item.getAssessedPrice());
		}
		if (msg) {
			pc.sendPackets(new S_SystemMessage("系統自動回收身上無中獎的賭肥票券。"));
		}
		this.writeH(0x07); // TODO 商店販賣金額顯示封包更新
	}

	@Override
	public byte[] getContent() {
		return this.getBytes();
	}

	@Override
	public String getType() {
		return S_SHOP_BUY_LIST;
	}
}